using System.Collections.Generic;
using UnityEngine;

namespace Brick
{
	public class LevelMove : MonoBehaviour
	{
		public GameObject deathPanel;

		public LevelState levelState;

		private List<Transform> lineList = new List<Transform>();

		private LevelCreate levelcreate;

		private void Start()
		{
			levelcreate = GetComponent<LevelCreate>();
			lineList = GetAllChild(base.transform);
			CreateLevel();
		}

		private void Update()
		{
			if (levelState == LevelState.life && UnityEngine.Input.GetKeyDown(KeyCode.Escape))
			{
				levelState = LevelState.pause;
				deathPanel.SetActive(value: true);
				Time.timeScale = 0f;
			}
			else if (levelState == LevelState.pause && UnityEngine.Input.GetKeyDown(KeyCode.Escape))
			{
				levelState = LevelState.life;
				deathPanel.SetActive(value: false);
				Time.timeScale = 1f;
			}
		}

		private List<Transform> GetAllChild(Transform fatherObj)
		{
			List<Transform> list = new List<Transform>();
			int childCount = fatherObj.childCount;
			for (int i = 0; i < childCount; i++)
			{
				list.Add(fatherObj.GetChild(i));
			}
			return list;
		}

		private void CreateLevel()
		{
			Transform transform = lineList[lineList.Count - 1];
			List<Transform> allChild = GetAllChild(transform);
			Transform transform2 = levelcreate.CreateEnemy();
			int num = UnityEngine.Random.Range(0, transform.childCount);
			transform2.position = transform.GetChild(num).position;
			transform2.parent = transform.GetChild(num);
			for (int i = 0; i < allChild.Count; i++)
			{
				if (i != num)
				{
					Transform transform3 = levelcreate.PaneFactory();
					if (transform3 != null)
					{
						transform3.position = allChild[i].position;
						transform3.parent = allChild[i];
					}
				}
			}
		}

		public void LevelGetUp()
		{
			Vector3 position = lineList[lineList.Count - 1].position;
			for (int num = lineList.Count - 1; num >= 0; num--)
			{
				if (num == 0)
				{
					lineList[num].position = position;
				}
				else
				{
					lineList[num].position = lineList[num - 1].position;
				}
			}
			DestroyStunt();
			lineList.Add(lineList[0]);
			lineList.RemoveAt(0);
			if (Death())
			{
				levelState = LevelState.die;
				deathPanel.SetActive(value: true);
			}
		}

		private void DestroyStunt()
		{
			Transform[] componentsInChildren = lineList[0].GetComponentsInChildren<Transform>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if (componentsInChildren[i].tag == "Stunt")
				{
					UnityEngine.Object.Destroy(componentsInChildren[i].gameObject);
				}
			}
		}

		private bool Death()
		{
			Transform[] componentsInChildren = lineList[0].GetComponentsInChildren<Transform>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if (componentsInChildren[i].tag == "Enemy")
				{
					return true;
				}
			}
			return false;
		}
	}
}

